<?php

namespace Pyaf\GymBundle\Repository;

use Doctrine\ORM\EntityRepository;

class CuotaRepository extends EntityRepository
{
    public function findAllAlumnosDeben() {
        return $this->getEntityManager()
                ->createQuery("SELECT c FROM PyafGymBundle:Cuota c WHERE c.estado = false GROUP BY c.alumno")                             
                ->getResult();
    }
    
    public function findAllAlumnosNoDeben() {
        
        $cuota = new \Pyaf\GymBundle\Entity\Cuota();
        $cuota = $this->findAllAlumnosDeben();
        $valores = "";
        
        if(count($cuota) > 0){
            $i = count($cuota);
            foreach ($cuota as $value) {
                $valores .= $value->getAlumno()->getId();
                if((--$i)!=0){
                    $valores .= ',';
                }
            }

            return $this->getEntityManager()
                    ->createQuery("SELECT c FROM PyafGymBundle:Cuota c WHERE c.alumno NOT IN ($valores) GROUP BY c.alumno")                             
                    ->getResult();
        } else {
            
            return $this->getEntityManager()
                    ->createQuery("SELECT c FROM PyafGymBundle:Cuota c WHERE c.estado = true GROUP BY c.alumno")                             
                    ->getResult();
        }
    }
    
    public function findAllByAlumno($id_alumno){
        return $this->getEntityManager()
                ->createQuery("SELECT c FROM PyafGymBundle:Cuota c WHERE c.alumno = $id_alumno AND c.estado = false ORDER BY c.mes_abono ASC")                             
                ->getResult();
    }
    
    public function AlumnosQueDebenMes($cuotaMes) {
        $alumnosPagaronMes = $this->getEntityManager()
                            ->createQuery("SELECT c FROM PyafGymBundle:Cuota c WHERE c.mes_abono = '$cuotaMes'")                             
                            ->getResult();
        $valores = "";
        if(count($alumnosPagaronMes) > 0){
            $i = count($alumnosPagaronMes);
            foreach ($alumnosPagaronMes as $value) {
                $valores .= $value->getAlumno()->getId();
                if((--$i)!=0){
                    $valores .= ',';
                }
            }
            return $this->getEntityManager()
                        ->createQuery("SELECT a FROM PyafGymBundle:Alumno a WHERE a.id NOT IN ($valores)")                             
                        ->getResult();
        } else {
            
            return $this->getEntityManager()
                        ->createQuery("SELECT a FROM PyafGymBundle:Alumno a")                             
                        ->getResult();
        }
    }
}
